﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Eff.Extensions;
using Redmine.Core.Locals;

namespace Redmine.Core.Libs.Redmine
{
    public class MenuManager
    {
        static MenuManager()
        {
            MenuDic = new Dictionary<string, dynamic>(50);
        }

        public static Dictionary<string, dynamic> MenuDic { get; set; }

        public static void ExtractNodeDetails(MenuItem node, out string caption, out object url, out bool selected)
        {
            //todo:project include
            var urltype = node.Url.GetType();
            if (urltype == typeof(Dictionary<string, string>))
            {
                url = node.Url;
            }
            else //if (urltype == typeof(string))
            {
                url = node.Url.ToString()[0].Equals(":") ? Const.Path[node.Url.ToString().Remove(0)] : node.Url.ToString();
            }
            caption = node.Caption();
            selected = false;
        }
    }

    public class MenuItem : TreeNode
    {
        /// <summary>
        /// 
        /// </summary>
        public object Url { get; private set; }
        public string Name { get; private set; }
        public string Param { get; private set; }
        public string Condition { get; private set; }
        public string Parent { get; private set; }
        public string ChlidMenus { get; private set; }
        public bool Last { get; private set; }
        private readonly string _caption;
        private readonly Dictionary<string, string> _html;
        //        :name, :url, :param, :condition, :parent, :child_menus, :last
        public MenuItem(string name, object url, Dictionary<MenuItemOptionKey, object> options = null)
            : base(name)
        {
            object objTem;
            Url = url;
            Name = name;
            if (options == null) return;
            Param = options.ContainsKey(MenuItemOptionKey.Param) ? options[MenuItemOptionKey.Param].ToString() : "id";
            _caption = options.ContainsKey(MenuItemOptionKey.Caption) ? options[MenuItemOptionKey.Caption].ToString() : string.Empty;
            _html = options.ContainsKey(MenuItemOptionKey.Html) ? (Dictionary<string, string>)options[MenuItemOptionKey.Html] : new Dictionary<string, string>(0);
            if (_html.ContainsKey("class"))
                _html["class"] += " " + Name;
            else
                _html.Add("class", Name);
            if (options.TryGetValue(MenuItemOptionKey.Caption, out objTem))
                _caption = objTem.ToString();
            Last = options.ContainsKey(MenuItemOptionKey.Last) && (bool)options[MenuItemOptionKey.Last];
            //todo @parent @child_menus
        }

        public string Caption()
        {
            //todo project
            if (_caption.IsEmptyOrNull())
                return "Label_" + Name;
            return Lang.ResourceManager.GetString(_caption);
        }

    }

    public class Mapper
    {
        private TreeNode _menuItems;
        private string _menu;
        public Mapper(string menu, ref Dictionary<string, TreeNode> items)
        {
            TreeNode tem;
            if (!items.TryGetValue(menu, out tem))
            {
                tem = new TreeNode("root");
                items.Add(menu, tem);
            }

            _menu = menu;
            _menuItems = tem;
        }
    }


    public enum MenuItemOptionKey : short
    {
        Param,
        Caption,
        Condition,
        Html,
        Child,
        Last
    }
}
